<?php 
namespace app\home\controller;

use think\Controller;

use think\Input;

use think\Db;

use think\Request;

use think\Session;

	class Address extends Controller{

		public function index(){

			//查询用户信息
			$user = Session("userad");

			$user_id = $user['user_id'];

			if($user){
				
				$sql=db('address')->where('user_id',$user_id)->select();

				if($sql){

					$this->assign('sql',$sql);
					
				}else{
					
					//如果该用户没有地址，就跳到添加地址页面
					// return $this->fetch('Cydia/index');
				}
					
			}else{
				
				//如果该用户没有关注，就让他关注
				// return $this->fetch('index/index');
			}
			return $this->fetch();
			
		}


		public function defaul(){

			if(request()->isPost()){

				$user = Session("userad");

				$user_id = $user['user_id'];

				//修改默认时，先把该用户原来的地址ismr改为0,
				if($user){
					
					$result = db('address')->where(['user_id'=>$user_id,'ismr'=>1])->update(['ismr'=>'0']);
						
					$data = input('post.');

					$sql = db('address')->where('id',$data['id'])->update(['ismr'=>'1']);

					if($sql==1){
						return ['code'=>1,'msg'=>'success'];
					}else{
						return ['code'=>0,'msg'=>'error'];
					}
				
				}else{
					return ['code'=>0,'msg'=>'还没登录'];
				}
				
			}else{
				return ['code'=>0,'msg'=>'发送失败'];
			}

		}


		public function del(){

			if(request()->isPost()){

				$data = input('post.');
			
				$sqll = db('address')->where('id',$data['id'])->find();

				if($sqll){
					
					if($sqll['ismr']==1){
						return ['code'=>0,'msg'=>'已设为默认，不可删除'];
					}else{

						$sql = db('address')->where('id',$data['id'])->delete();

						if($sql==1){
							return ['code'=>1,'msg'=>'操作成功'];
						}else{
							return ['code'=>0,'msg'=>'操作失败'];
						}

					}
				}else{
					return ['code'=>0,'msg'=>'error'];
				}

				
			}

		}
	}

 ?>